from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from fastapi.responses import Response

from common.database import get_db
from core.response import resp_200, resp_400
from core.logger import logger
from typing import Dict, List


from app.notice_center.handlers.nc_log_alarm_handler import NCLogAlarmHandler
from app.notice_center.handlers.nc_log_alarm_record_handler import NCLogAlarmRecordHandler
from app.notice_center.handlers.nc_log_alarm_notice_handler import NCLogAlarmNoticeHandler
from app.notice_center.handlers.nc_n9e_alarm_handler import NCN9eAlarmHandler
from app.notice_center.handlers.nc_n9e_alarm_record_handler import NCN9eAlarmRecordHandler
from app.notice_center.handlers.nc_n9e_callback_container_cpu_handler import NCN9eCallbackContainerCpuHandler
from app.notice_center.handlers.nc_n9e_callback_pod_heal_handler import NCN9eCallbackPodHealHandler
from app.notice_center.handlers.nc_n9e_callback_node_mem_handler import NCN9eCallbackNodeMemHandler
from app.notice_center.handlers.nc_n9e_callback_cassandra_recovery_handler import NCN9eCallbackCassandraRecoveryHandler
from app.notice_center.handlers.nc_n9e_callback_rabbitmq_queue_handler import NCN9eCallbackRabbitmqQueueHandler
from app.notice_center.handlers.nc_prometheus_metrics_handler import NCPrometheusMetricsHandler
from core.security import permission_checker

from app.notice_center.schemas import *


router = APIRouter()


# #### 日志告警webhook操作 ####
# @router.post('/log/webhook/', summary="日志告警的webhook", description="通知中心-日志")
# async def post_nc_log_alarm_webhook(data: List[PostLogAlarmWebhook], db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_handler = NCLogAlarmHandler()
#     data = [tmp.dict() for tmp in data]
#     result = nc_log_alarm_handler.post_log_alarm(db, data)
#     if not result:
#         message = "post log webhook failed"
#         logger.error(f"{message} => data:{data}")
#         return resp_400(message)
#     return resp_200()







# #### 日志告警记录操作 ####
# @router.get('/log/alarm/record/', summary="根据表字段-模糊匹配, 获取日志告警记录信息", description="通知中心-日志")
# async def get_nc_log_alarm_record_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
#         db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_record_handler = NCLogAlarmRecordHandler()
#     status, result = nc_log_alarm_record_handler.get_log_alarm_record_search(db, page, per_page, field, value)
#     if not status:
#         message = "get log alarm record search failed"
#         logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
#         return resp_400(message)
#     return resp_200(result)


# @router.get('/log/alarm/record/match/', summary="根据表字段-完全匹配, 获取日志告警记录信息", description="通知中心-日志")
# async def get_nc_log_alarm_record_match(field: str = Query(..., description="需要查询的字段"), value: str = Query(..., description="字段的值"),
#                                 page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
#                                 db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_record_handler = NCLogAlarmRecordHandler()
#     status, result = nc_log_alarm_record_handler.get_log_alarm_record_match(db, page, per_page, field, value)
#     if not status:
#         message = "get log alarm record match failed"
#         logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
#         return resp_400(message)
#     return resp_200(result)



# @router.put('/log/alarm/record/', summary="修改日志告警记录信息", description="通知中心-日志")
# async def put_nc_log_alarm_record(data: PutLogAlarmRecord, db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_record_handler = NCLogAlarmRecordHandler()
#     result = nc_log_alarm_record_handler.update_log_alarm_record(db, data.dict())
#     if not result:
#         message = "put log alarm record failed"
#         logger.error(f"{message} => data:{data.dict()}")
#         return resp_400(message)
#     return resp_200()


# @router.delete('/log/alarm/record/', summary="删除日志告警记录信息", description="通知中心-日志")
# async def delete_nc_log_alarm_record(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_record_handler = NCLogAlarmRecordHandler()
#     result = nc_log_alarm_record_handler.delete_log_alarm_record(db, data.dict())
#     if not result:
#         message = "delete log alarm record failed"
#         logger.error(f"{message} => data:{data.dict()}")
#         return resp_400(message)
    # return resp_200()


@router.get('/log/alarm/status/', summary="日志告警系统的状态", description="通知中心-日志")
async def get_nc_log_alarm_status(db: Session = Depends(get_db)):
    nc_log_alarm_record_handler = NCLogAlarmRecordHandler()
    result = nc_log_alarm_record_handler.get_nc_log_alarm_status(db, data.dict())
    if not result:
        message = "get log alarm status failed"
        logger.error(message)
        return resp_400(message)
    return resp_200()






# #### 日志告警通知操作 ####
# @router.get('/log/alarm/notice/', summary="根据表字段-模糊匹配, 获取日志告警通知信息", description="通知中心-日志")
# async def get_nc_log_alarm_notice_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
#         db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_notice_handler = NCLogAlarmNoticeHandler()
#     status, result = nc_log_alarm_notice_handler.get_log_alarm_notice_search(db, page, per_page, field, value)
#     if not status:
#         message = "get log alarm notice search failed"
#         logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
#         return resp_400(message)
#     return resp_200(result)


# @router.get('/log/alarm/notice/match/', summary="根据表字段-完全匹配, 获取日志告警通知信息", description="通知中心-日志")
# async def get_nc_log_alarm_notice_match(field: str = Query(..., description="需要查询的字段"), value: str = Query(..., description="字段的值"),
#                                 page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
#                                 db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_notice_handler = NCLogAlarmNoticeHandler()
#     status, result = nc_log_alarm_notice_handler.get_log_alarm_notice_match(db, page, per_page, field, value)
#     if not status:
#         message = "get log alarm notice match failed"
#         logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
#         return resp_400(message)
#     return resp_200(result)



# @router.post('/log/alarm/notice/', summary="提交日志告警信息", description="通知中心-日志")
# async def post_nc_log_alarm_notice(data: PostLogAlarmNotice, db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_notice_handler = NCLogAlarmNoticeHandler()
#     result = nc_log_alarm_notice_handler.add_log_alarm_notice(db, data.dict())
#     if not result:
#         message = "post log alarm notice failed"
#         logger.error(f"{message} => data:{data.dict()}")
#         return resp_400(message)
#     return resp_200()




# @router.put('/log/alarm/notice/', summary="修改日志告警通知信息", description="通知中心-日志")
# async def put_nc_log_alarm_notice(data: PutLogAlarmNotice, db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_notice_handler = NCLogAlarmNoticeHandler()
#     result = nc_log_alarm_notice_handler.update_log_alarm_notice(db, data.dict())
#     if not result:
#         message = "put log alarm notice failed"
#         logger.error(f"{message} => data:{data.dict()}")
#         return resp_400(message)
#     return resp_200()



@router.put('/log/alarm/maintain/', summary="修改日志告警维护时间", description="通知中心-日志")
async def put_nc_log_alarm_maintain(data: PutLogAlarmMaintain, db: Session = Depends(get_db), user=Depends(permission_checker)):
    nc_log_alarm_notice_handler = NCLogAlarmNoticeHandler()
    result = nc_log_alarm_notice_handler.update_log_alarm_maintain(db, data.dict())
    if not result:
        message = "put log alarm maintain failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()



# @router.delete('/log/alarm/notice/', summary="删除日志告警通知信息", description="通知中心-日志")
# async def delete_nc_log_alarm_notice(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_log_alarm_notice_handler = NCLogAlarmNoticeHandler()
#     result = nc_log_alarm_notice_handler.delete_log_alarm_notice(db, data.dict())
#     if not result:
#         message = "delete log alarm notice failed"
#         logger.error(f"{message} => data:{data.dict()}")
#         return resp_400(message)
#     return resp_200()








#### n9e告警通知操作 ####
@router.post('/n9e/webhook/', summary="n9e告警通知的webhook", description="通知中心-n9e")
async def post_nc_n9e_alarm_webhook(data: Dict, db: Session = Depends(get_db)):
    nc_n9e_alarm_handler = NCN9eAlarmHandler()
    result = nc_n9e_alarm_handler.post_n9e_alarm(db, data)
    if not result:
        message = "post n9e webhook failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()



# @router.get('/n9e/alarm/record/', summary="根据表字段-模糊匹配, 获取n9e告警记录信息", description="通知中心-n9e")
# async def get_nc_n9e_alarm_record_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
#         db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_n9e_alarm_record_handler = NCN9eAlarmRecordHandler()
#     status, result = nc_n9e_alarm_record_handler.get_n9e_alarm_record_search(db, page, per_page, field, value)
#     if not status:
#         message = "get n9e alarm record search failed"
#         logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
#         return resp_400(message)
#     return resp_200(result)


# @router.get('/n9e/alarm/record/match/', summary="根据表字段-完全匹配, 获取n9e告警记录信息", description="通知中心-n9e")
# async def get_nc_n9e_alarm_record_match(field: str = Query(..., description="需要查询的字段"), value: str = Query(..., description="字段的值"),
#                                 page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
#                                 db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_n9e_alarm_record_handler = NCN9eAlarmRecordHandler()
#     status, result = nc_n9e_alarm_record_handler.get_n9e_alarm_record_match(db, page, per_page, field, value)
#     if not status:
#         message = "get n9e alarm record match failed"
#         logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
#         return resp_400(message)
#     return resp_200(result)



@router.put('/n9e/alarm/record/', summary="修改n9e告警记录信息", description="通知中心-n9e")
async def put_nc_n9e_alarm_record(data: PutN9eAlarmRecord, db: Session = Depends(get_db), user=Depends(permission_checker)):
    nc_n9e_alarm_record_handler = NCN9eAlarmRecordHandler()
    result = nc_n9e_alarm_record_handler.update_n9e_alarm_record(db, data.dict())
    if not result:
        message = "put n9e alarm record failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


# @router.delete('/n9e/alarm/record/', summary="删除n9e告警记录信息", description="通知中心-n9e")
# async def delete_nc_n9e_alarm_record(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
#     nc_n9e_alarm_record_handler = NCN9eAlarmRecordHandler()
#     result = nc_n9e_alarm_record_handler.delete_n9e_alarm_record(db, data.dict())
#     if not result:
#         message = "delete n9e alarm record failed"
#         logger.error(f"{message} => data:{data.dict()}")
#         return resp_400(message)
#     return resp_200()


@router.get('/n9e/alarm/status/', summary="n9e告警系统的状态", description="通知中心-n9e")
async def get_nc_n9e_alarm_status(db: Session = Depends(get_db)):
    nc_n9e_alarm_record_handler = NCN9eAlarmRecordHandler()
    result = nc_n9e_alarm_record_handler.get_nc_n9e_alarm_status(db)
    if not result:
        message = "get alarm status failed"
        logger.error(message)
        return resp_400(message)
    return resp_200()






#### n9e告警自愈接口 ####
@router.post('/n9e/heal/node/mem/', summary="k8s的node节点内存过高, 删除内存最高的pod", description="通知中心-n9e")
async def post_nc_n9e_heal_node_mem(data: dict, token: str = Query(..., description="接口token"), db: Session = Depends(get_db)):
    callback_node_mem_handler = NCN9eCallbackNodeMemHandler()
    result = callback_node_mem_handler.post_nc_n9e_heal_node_mem(db, data, token)
    if not result:
        message = "post heal node mem failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()

@router.post('/n9e/heal/cassandra/recovery/', summary="cassandra内存过高时删除异常的任务", description="通知中心-n9e")
async def post_nc_n9e_heal_cassandra_recovery(data: dict, token: str = Query(..., description="接口token"), db: Session = Depends(get_db)):
    callback_cassandra_recovery_handler = NCN9eCallbackCassandraRecoveryHandler()
    result = callback_cassandra_recovery_handler.post_nc_n9e_heal_cassandra_recovery(db, data, token)
    if not result:
        message = "post heal cassandra recovery failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()


@router.post('/n9e/heal/pod/restart/', summary="pod重启次数后还没恢复,删除pod重启", description="通知中心-n9e")
async def post_nc_n9e_heal_pod_restart(data: dict, token: str = Query(..., description="接口token"), db: Session = Depends(get_db)):
    callback_pod_heal_handler = NCN9eCallbackPodHealHandler()
    result = callback_pod_heal_handler.post_nc_n9e_heal_pod_restart(db, data, token)
    if not result:
        message = "post heal pod restart failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()


@router.post('/n9e/heal/container/cpuusage/', summary="生产业务服务cpu平均使用率过高", description="通知中心-n9e")
async def post_nc_n9e_heal_container_cpu_usage(data: dict, token: str = Query(..., description="接口token"), db: Session = Depends(get_db)):
    callback_container_cpu_handler = NCN9eCallbackContainerCpuHandler()
    result = callback_container_cpu_handler.post_nc_n9e_heal_container_cpu_usage(db, data, token)
    if not result:
        message = "post container cpu usage failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()


@router.post('/n9e/heal/rabbitmq/queue/', summary="rabbitmq队列消息祖塞", description="通知中心-n9e")
async def post_nc_n9e_heal_rabbitmq_queue(data: dict, token: str = Query(..., description="接口token"), db: Session = Depends(get_db)):
    callback_rabbitmq_queue_handler = NCN9eCallbackRabbitmqQueueHandler()
    result = callback_rabbitmq_queue_handler.post_nc_n9e_heal_rabbitmq_queue(db, data, token)
    if not result:
        message = "post rabbitmq queue failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()





#### prometheus的metrics ####
@router.get('/metrics/', summary="获取prometheus的metrics格式信息", description="通知中心-指标")
async def get_all_metrics(db: Session = Depends(get_db)):
    nc_prometheus_metrics_handler = NCPrometheusMetricsHandler()
    result = nc_prometheus_metrics_handler.get_all_metrics(db)
    if not result:
        message = "get prometheus metrics failed"
        logger.error(message)
        return resp_400(message)
    return Response(content=result, media_type="text/plain") 